﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Core.Base;
using System.ComponentModel;
using Base;
using System.Drawing;
using Core.Component;
using Core;

namespace Components
{
    //public class SGVTextBox: MaskedTextBox, IBindingControl, ISGVControl
    public class SGVNumericTextBox : TextBox, IBindingControl, ISGVControl
    {
        public SGVNumericTextBox()
            :base()
        {
            this.TextAlign = HorizontalAlignment.Right;
            this.CausesValidation = false;
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.ShortcutsEnabled = true;
            this.GotFocus += new EventHandler(SGVComboBox_GotFocus);
            this.LostFocus += new EventHandler(SGVComboBox_LostFocus);

            this.KeyPress += new KeyPressEventHandler(SGVNumericTextBox_KeyPress);
        }

        public Core.Component.UIComponent component
        {
            set;
            get;
        }

        void SGVNumericTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            //e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
            Keys k = (Keys)char.ToUpper(e.KeyChar);
            e.Handled = suppressKeyPress(k);
        }

        /// <summary>
        /// Processes the data entered into a textbox to determine if it is numeric.
        /// </summary>
        /// <param name="KeyCode">The KeyCode that was typed into the textbox.</param>
        /// <returns>False if the value entered was numeric, otherwise true.</returns>
        private bool suppressKeyPress(Keys KeyCode)
        {
            if (KeyCode < Keys.D0 || KeyCode > Keys.D9 &&
                KeyCode < Keys.NumPad0 || KeyCode > Keys.NumPad9)
            {
                if (KeyCode != Keys.Back &&
                    KeyCode != Keys.Delete &&
                    KeyCode != Keys.Left &&
                    KeyCode != Keys.Right)
                {
                    return true;
                }
            }
            if (this.Text.Contains('.') && KeyCode == Keys.Decimal)
            {
                return false;
            }

            if (Control.ModifierKeys == Keys.Shift)
                return true;

            return false;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.A))
            {
                this.SelectAll();
            }
            else if (keyData == (Keys.Control | Keys.C))
            {
                this.Copy();
            }
            else if (keyData == (Keys.Control | Keys.V))
            {
                this.Paste();
            }
            else if (keyData == (Keys.Control | Keys.X))
            {
                this.Cut();
            }
            else if (keyData == (Keys.Control | Keys.Z))
            {
                this.Undo();
            }
            else if (keyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        void SGVComboBox_LostFocus(object sender, EventArgs e)
        {
            SGVControlHelper.ControlLostFocus(this);

            this.Text = AppContext.ConvertStringToNumber(this.Text);
        }

        void SGVComboBox_GotFocus(object sender, EventArgs e)
        {
            SGVControlHelper.ControlFocus(this);
            this.SelectAll();
        }

        #region IBindingControl
        BaseModule module;
        BaseEntity entity;
        String _bindingPropertyName = "Text";

        String _bindingTable;
        String _bindingColumn;
        String _dataSourceTable;
        String _dataSourceDisplayColumn;
        String _dataSourceKeyColumn;
        String _dataSourceColumns;
        String _sGVShortcutString = "Nhập vào chuỗi dạng số";
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String SGVShortcutString
        {
            get { return _sGVShortcutString; }
            set { _sGVShortcutString = value; }
        }
        String _sGVTag;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string SGVTag
        {
            get
            {
                return _sGVTag;
            }
            set
            {
                _sGVTag = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingPropertyName
        {
            get
            {
                return _bindingPropertyName;
            }
            set
            {
                _bindingPropertyName = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingTable
        {
            get
            {
                return _bindingTable;
            }
            set
            {
                _bindingTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingColumn
        {
            get
            {
                return _bindingColumn;
            }
            set
            {
                _bindingColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceTable
        {
            get
            {
                return _dataSourceTable;
            }
            set
            {
                _dataSourceTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceDisplayColumn
        {
            get
            {
                return _dataSourceDisplayColumn;
            }
            set
            {
                _dataSourceDisplayColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceKeyColumn
        {
            get
            {
                return _dataSourceKeyColumn;
            }
            set
            {
                _dataSourceKeyColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String DataSourceColumns
        {
            get
            {
                return _dataSourceColumns;
            }
            set
            {
                _dataSourceColumns = value;
            }
        }

        public void RegisterModule(Core.Base.BaseModule _module)
        {
            if (_module != null)
            {
                this.module = _module;
                this.entity = _module.entity;
            }
        }

        #endregion

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            e.Graphics.DrawString("Hahaha", this.Font, Brushes.Blue, new PointF(0, 0));
        }

        public void Init()
        {
            this.TextChanged += new EventHandler(SGVNumericTextBox_TextChanged);
        }

        void SGVNumericTextBox_TextChanged(object sender, EventArgs e)
        {
            if(this.Focused == false)
                UpdateFormat();
        }

        void SGVNumericTextBox_BindingContextChanged(object sender, EventArgs e)
        {
        }


        public void UpdateFormat()
        {
            this.Text = AppContext.ConvertStringToNumber(this.Text);
            this.Refresh();
        }

        public UIComponent Serialize()
        {
            UIComponent component = new UIComponent();
            SGVControlHelper.CreateUIComponentFromControl(component, this);

            if (component != null)
            {
                component.Value = this.Text;
            }
            return component;
        }

        public void Deserialize(UIComponent component)
        {
            throw new NotImplementedException();
        }

        UIComponent ISGVControl.component
        {
            set;
            get;
        }

        string ISGVControl.SGVTag
        {
            set;
            get;
        }

        string ISGVControl.SGVShortcutString
        {
            set;
            get;
        }

        void ISGVControl.RegisterModule(BaseModule module)
        {
            throw new NotImplementedException();
        }

        void ISGVControl.UpdateFormat()
        {
            
        }

        void ISGVControl.Init()
        {
            throw new NotImplementedException();
        }
    }
}
